package com.lanwf.admin.sys.login;

import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.lanwf.admin.BaseController;
import com.lanwf.common.model.SysAdmin;
import com.lanwf.common.model.SysSession;
import com.lanwf.admin.interceptor.AdminLockscreenInterceptor;
import com.lanwf.admin.interceptor.AdminLoginInterceptor;
import com.lanwf.admin.utils.CommonUtil;
import com.lanwf.admin.utils.Cons;
import com.lanwf.admin.utils.TripleDes;
import org.apache.commons.lang3.time.DateUtils;

import java.util.Date;

/**
 * 登录控制层
 */
@Path(value = "/admin/login", viewPath = "/admin/login")
public class LoginController extends BaseController {

    @Inject
    LoginService service;

    /**
     * 显示登录界面
     */
    @Clear({AdminLoginInterceptor.class, AdminLockscreenInterceptor.class})
    public void index() {
        set(Cons.PROJECT_NAME+"username", getCookie(Cons.PROJECT_NAME+"username"));
        set(Cons.PROJECT_NAME+"password", TripleDes.decrypt3DES(getCookie(Cons.PROJECT_NAME+"password"), Cons.KEY));
        set(Cons.PROJECT_NAME+"remember", getCookie(Cons.PROJECT_NAME+"remember"));

        render("index.html");
    }
    @Clear
    public void doLogin() {
        String username = get("username", "");
        String encryptText = get("password", "");
        String remember = get("remember", "off");
         boolean b = validateCaptcha("vercode");
            if (!b) {
            renderJson(Ret.fail("msg", "验证码输入有误!"));
            return;
        }
        Ret ret = service.doLogin(username, encryptText);
        if (ret.isOk()) {
            if (remember.equals("on")) {
                setCookie(Cons.PROJECT_NAME+"username", username, Cons.ADMIN_COOKIE_expires, true);
                setCookie(Cons.PROJECT_NAME+"password", TripleDes.encrypt3DES(ret.getStr("password"), Cons.KEY), Cons.ADMIN_COOKIE_expires, true);
                setCookie(Cons.PROJECT_NAME+"remember", remember, Cons.ADMIN_COOKIE_expires, true);
            } else {
                removeCookie(Cons.PROJECT_NAME+"username");
                removeCookie(Cons.PROJECT_NAME+"password");
                removeCookie(Cons.PROJECT_NAME+"remember");
            }
            String randomUUID = StrKit.getRandomUUID();
            setCookie(Cons.ADMIN_COOKIE, randomUUID, Cons.ADMIN_COOKIE_expires, true);
            saveSession(randomUUID, ret.getInt("id"));
            removeCookie(Cons.LOCKSCREEN);//登录清空锁屏
            CommonUtil.setCache("listMenus", null);//清空菜单
        }
        renderJson(ret);
    }

    private void saveSession(String uuid, Integer accountId) {
        SysSession s = new SysSession();
        s.setId(uuid);
        s.setAccountId(accountId);
        s.setCreateTime(new Date());
        s.setExpires(DateUtils.addHours(new Date(), 6));
        s.save();
    }

    /**
     * 退出登录
     */
    @Clear
    public void logout() {
        Db.delete("delete from sys_session where id = ? ", getCookie());
        removeCookie(Cons.ADMIN_COOKIE);
        renderJson(Ret.ok());
    }

    public void reLogin() {
        String cookie = getCookie();
        SysSession first = SysSession.dao.findFirst("select * from sys_session where id = ? ", cookie);
        //退出登录
        logout();
        if (first == null) {
            renderJson(Ret.fail().set("msg", "会话已过期,去登录页登录"));
            return;
        }
        SysAdmin account = SysAdmin.dao.findById(first.getAccountId());
        if (account.getStatus().intValue() == 1) {
            renderJson(Ret.fail("msg", "账号已被冻结"));
            return;
        }
        String randomUUID = StrKit.getRandomUUID();
        setCookie(Cons.ADMIN_COOKIE, randomUUID, Cons.ADMIN_COOKIE_expires, true);
        saveSession(randomUUID, first.getAccountId());
        CommonUtil.setCache("listMenus", null);//清空菜单
        renderJson(Ret.ok().set("msg", "重新登录成功"));
    }




    @Clear
    public void captcha() {
        renderCaptcha();
    }

}


